package com.ruoyi.walmart.service.shipments.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.walmart.domain.shipments.ShipmentStock;
import com.ruoyi.walmart.domain.shipments.dto.ShipmentStockFilters;
import com.ruoyi.walmart.dto.ShipmentItemDTO;
import com.ruoyi.walmart.mapper.shipments.ShipmentStockMapper;
import com.ruoyi.walmart.service.shipments.ShipmentItemService;
import com.ruoyi.walmart.service.shipments.ShipmentStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShipmentStockServiceImpl extends ServiceImpl<ShipmentStockMapper, ShipmentStock> implements ShipmentStockService {
    @Autowired
    private ShipmentItemService shipmentItemService;
    @Autowired
    private ShipmentStockMapper shipmentStockMapper;

    /**
     * 手动绑定备货计划
     * @param ShipmentStockFilters
     * @return
     */
    @Override
    public void bingShipmentStock(ShipmentStockFilters ShipmentStockFilters) {
        String shipmentId = ShipmentStockFilters.getShipmentId();
        String stockId = ShipmentStockFilters.getStockId();
        Long authId = ShipmentStockFilters.getAuthId();

        // 获取shipment item列表
        ShipmentItemDTO shipmentItem = new ShipmentItemDTO();
        shipmentItem.setShipmentId(shipmentId);
        List<ShipmentItemDTO> shipmentItemList = shipmentItemService.getShipmentItemDTO(shipmentItem);

        // 前端传过来的 stockProductDetails 列表
        List<ShipmentStockFilters.StockProductDetails> productDetailsList = ShipmentStockFilters.getStockProductDetails();
//        if(shipmentItemList.size() != productDetailsList.size()){
//            throw new ServiceException("选择的备货计划商品条数与入库订单商品条数不一致，请核查");？？？当一个备货计划生成多个货件时，该校验有误
//        }

        // 批量插入的列表
        List<ShipmentStock> batchInsertList = new ArrayList<>();

        for (ShipmentItemDTO item : shipmentItemList) {
            String sku = item.getSku();    // 假设 item 里有 sku 字段
            String gtin = item.getGtin();  // 假设 item 里有 gtin 字段
            Integer qty = Math.toIntExact(item.getItemQty());

            // 从 productDetailsList 里查找匹配的 productCost 和 firstLeg
            ShipmentStockFilters.StockProductDetails matchedDetails = null;
            for (ShipmentStockFilters.StockProductDetails details : productDetailsList) {
                if (details.getSku().equals(sku)) {
                    matchedDetails = details;
                    break;
                }
            }

            if (matchedDetails == null) {
                continue; // 没找到对应的，跳过
            }

            // 构造 WalmartShipmentStock 实体
            ShipmentStock record = new ShipmentStock();
            record.setId(IdWorker.getId());
            record.setShipmentId(shipmentId);
            record.setStockId(stockId);
            record.setSku(sku);
            record.setGtin(gtin);
            record.setQty(qty);
            record.setAuthId(authId);
            record.setProductCost(matchedDetails.getProductCost());
            record.setFirstLeg(matchedDetails.getFirstLeg());

            batchInsertList.add(record);
        }

        // 批量插入数据库
        if (!batchInsertList.isEmpty()) {
            shipmentStockMapper.batchInsertsShipmentStock(batchInsertList);
        }

    }
}
